﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Menu.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Menu
{
    public class AddMenuService : ServiceBase, IAddMenuService
    {
        protected readonly IMenuRepository menuRepository;

        public AddMenuService(ILogger<ServiceBase> logger, IOptions<WmsSettings> options, IMapper mapper, IMenuRepository menuRepository) : base(logger, options, mapper)
        {
            this.menuRepository = menuRepository;
        }

        public async Task<ActionResult<string>> AddMenu(string parentMenuId, UserInfo userInfo)
        {
            var result = new ActionResult<string>("添加菜单");
            try
            {
                if (string.IsNullOrWhiteSpace(parentMenuId))
                    return result.ArgumentError("请选择要创建菜单的结点");

                var parentId = Convert.ToInt64(parentMenuId);
                var menuPO = new MenuPO()
                {
                    NameCN = "新菜单",
                    NameEN = "NewMenu",
                    Icon = "newmenu.png",
                    TypeCode = "MenuType_Menu",
                    MenuPositionCode = "MenuPosition_Menu",
                    SortNumber = 1000,
                    Url = "",
                    Sql = "",
                    ConfigJson = "[]",
                    ParentId = parentId == 0 ? null : parentId,
                };

                var count = await menuRepository.Insert(menuPO, userInfo);
                if (count == 0)
                    return result.DbError("添加菜单异常");

                result.Data = menuPO.Id.ToString();
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
